﻿using System;
using System.IO;
using hello.csharp.domain;
using hello.csharp.helper;
using hello.csharp.helper.Enums;
using hello.csharp.storage.Db.Sqlite;
using SequentialGuid;

// ReSharper disable once IdentifierTypo
namespace hello.csharp.storage.app.sqlbuilder
{
    class Program
    {
        private static string _rootPath;

        public static void Main(string[] args)
        {
            Console.WriteLine($"Sql语句生成器v190322：按任意键工作....");
            Console.ReadKey();
            _rootPath = BuildSqlFilePath();
            Console.WriteLine($"Sql语句基础目录: {_rootPath}");

            var types = new Type[]
            {
                typeof(UserSnack), typeof(Role), typeof(User), typeof(WaterMeter)
            };
            foreach (var type in types)
            {
                Console.WriteLine();
                CreateSqlFile(type);
            }

            Console.WriteLine();
            Console.WriteLine($"Sql语句生成器工作结束。按任意键关闭....");
            Console.Read();
        }

        private static void CreateSqlFile(Type type)
        {
            var fileNames = BuildFileName(type);

            var file = Path.Combine(_rootPath, fileNames.Item1);
            var sql = SqlHelper.GetCreateTableSql(type, DatabaseType.SqLite);
            CreateTextFile(file, sql);
            Console.WriteLine(file);

            file = Path.Combine(_rootPath, fileNames.Item2);
            sql = SqlHelper.GetCreateTableSql(type, DatabaseType.PostgreSql);
            CreateTextFile(file, sql);
            Console.WriteLine(file);

            file = Path.Combine(_rootPath, fileNames.Item3);
            sql = SqlHelper.GetInsertSql(type, DatabaseType.SqLite);
            CreateTextFile(file, sql);
            Console.WriteLine(file);

            file = Path.Combine(_rootPath, fileNames.Item4);
            sql = SqlHelper.GetInsertSql(type, DatabaseType.PostgreSql);
            CreateTextFile(file, sql);
            Console.WriteLine(file);

            file = Path.Combine(_rootPath, fileNames.Item5);
            sql = SqlHelper.GetUpdateSql(type, DatabaseType.SqLite);
            CreateTextFile(file, sql);
            Console.WriteLine(file);

            file = Path.Combine(_rootPath, fileNames.Item6);
            sql = SqlHelper.GetUpdateSql(type, DatabaseType.PostgreSql);
            CreateTextFile(file, sql);
            Console.WriteLine(file);
        }

        private static void CreateTextFile(string file, string sql)
        {
            if (File.Exists(file))
                File.Delete(file);
            using (StreamWriter sr = File.CreateText(file))
            {
                sr.WriteLine(sql);
                sr.Close();
            }
        }

        private static (string, string, string, string, string, string) BuildFileName(Type type)
        {
            var sqlite = $"t.{type.Name}.sqlite.sql";
            var postgre = $"t.{type.Name}.postgre.sql";
            var insertSqlite = $"i.{type.Name}.sqlite.sql";
            var insertPostgre = $"i.{type.Name}.postgre.sql";
            var updateSqlite = $"u.{type.Name}.sqlite.sql";
            var updatePostgre = $"u.{type.Name}.postgre.sql";
            return new ValueTuple<string, string, string, string, string, string>(sqlite, postgre, insertSqlite, insertPostgre, updateSqlite, updatePostgre);
        }

        private static string BuildSqlFilePath()
        {
            var path = "../../../../";
            var spaceName = typeof(Program).Namespace;
            var index = spaceName.LastIndexOf(".app", StringComparison.Ordinal);
            spaceName = spaceName.Substring(0, index);
            path = Path.Combine(path, spaceName, "Sqls");
            path = new DirectoryInfo(path).FullName;
            return path;
        }
    }
}
